package com.swn.cm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swn.cm.service.TeacherService;
import com.swn.common.util.DateUtil;
import com.swn.common.util.JSONUtil;
import com.swn.common.util.PageListData;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	
	@Autowired
	private TeacherService teacherService;
	
	@RequestMapping("queryTeacherList")
	@ResponseBody
	public Map<String, Object> queryTeacherList(Integer start,Integer length,String signupForm){
		Map<String, Object> map = new HashMap<String, Object>();
		PageListData pageListData = new PageListData();
		try {
			pageListData.setStart(start);
			pageListData.setLength(length);
			@SuppressWarnings("unchecked")
			Map<String, Object> param = JSONUtil.getMapFromJsonString(signupForm);
			List<Map<String, Object>> teacherList = teacherService.queryTeacherList(pageListData,param);
			int total = pageListData.getCount();
			map.put("recordsTotal", total);                  // 总记录数
			map.put("recordsFiltered", total);               // 过滤后的总记录数
			map.put("data", teacherList);   
		} catch (Exception e) {
			logger.info("查询保存了");
		}
		return map;
		
	}
	
	/**
	 * 获取所有班级信息
	 * 
	 * @return List<Map<String,Object>>
	 *
	 * @Author 郭昌
	 * @Date 2018年7月12日 下午5:43:07 
	 * @Note <这里是方法的备注>
	 * @company 济南舒卫能生物技术有限公司
	 */
	@RequestMapping("queryClasses")
	@ResponseBody
	public List<Map<String, Object>> queryClasses(){
		List<Map<String, Object>> teacherList = teacherService.queryClasses();
		return teacherList;
	}
	
	/**
	 * 添加教师信息
	 * 
	 * @param addJsonStr
	 * @return String
	 *
	 * @Author 郭昌
	 * @Date 2018年7月12日 下午5:43:26 
	 * @Note <这里是方法的备注>
	 * @company 济南舒卫能生物技术有限公司
	 */
	@RequestMapping("/addTeacher")
	@ResponseBody
	public String addTeacher(String signupForm){
		
		@SuppressWarnings("unchecked")
		Map<String, Object> paraMap = JSONUtil.getMapFromJsonString(signupForm);
		paraMap.put("createtime", DateUtil.formatDateTime(DateUtil.getCurrentlyDate(), "yyyy-MM-dd"));
		int flag = teacherService.addTeacher(paraMap);
		if(flag==0){
			return "error";
		}
		return "success";
	}
	
	@RequestMapping("updateTeacher")
	@ResponseBody
	public String updateTeacher(String signupForm,long id){
		@SuppressWarnings("unchecked")
		Map<String,Object> paramMap = JSONUtil.getMapFromJsonString(signupForm);
		paramMap.put("teacher_id", id);
		if(teacherService.updateTeacher(paramMap)<=0){
			return "error";
		}
		return "success";
	}
	@RequestMapping("/queryTeacherById")
	@ResponseBody
	public Map<String, Object> queryTeacherById(Long id) throws Exception{
		Map<String, Object> teacherInfo = teacherService.queryTeacherById(id);
		return teacherInfo;
	}
	

	@RequestMapping("deleteTeacher")
	@ResponseBody
	public String deleteTeacher(long id){
		if(teacherService.deleteTeacher(id)<=0){
			return "error";
		}
		return "success";
	}
	
	Logger logger = Logger.getLogger(TeacherController.class);
	
}
